﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace Common
{
    public enum ExportFormat
    {
        ExcelFormat,
        XMLFormat
    }


    public class ExportDataHelper
    {
        // Methods
        public static void ExportDataSetToExcelWithTitle(string title, string[] _caption, DataSet ds, string[] cols, string FileName)
        {
            HttpResponse response = HttpContext.Current.Response;
            response.ContentEncoding = Encoding.GetEncoding("GB2312");
            response.AppendHeader("Content-Disposition", "attachment;filename=" + FileName);
            response.Charset = "UTF-8";
            string s = "";
            string str2 = "";
            int index = 0;
            DataTable table = ds.Tables[0];
            DataRow[] rowArray = table.Select("");
            response.ContentType = "application/ms-excel";
            response.Write(title + "\n");
            index = 0;
            while (index < (_caption.Length - 1))
            {
                s = s + _caption[index] + "\t";
                index++;
            }
            s = s + _caption[index] + "\n";
            response.Write(s);
            foreach (DataRow row in rowArray)
            {
                index = 0;
                while (index < (cols.Length - 1))
                {
                    if (table.Columns.Contains(cols[index]))
                    {
                        str2 = str2 + row[cols[index]].ToString().Replace("\t", "").Replace("\n", "").Replace("\r", "") + "\t";
                    }
                    index++;
                }
                str2 = str2 + row[cols[index]].ToString().Replace("\t", "").Replace("\n", "").Replace("\r", "") + "\n";
                response.Write(str2);
                str2 = "";
            }
            response.End();
        }

        public static void ExportDataSetToFile(DataSet ds, ExportFormat ef, string FileName)
        {
            HttpResponse response = HttpContext.Current.Response;
            response.ContentEncoding = Encoding.GetEncoding("GB2312");
            response.AppendHeader("Content-Disposition", "attachment;filename=" + FileName);
            response.Charset = "UTF-8";
            string s = "";
            string str2 = "";
            int num = 0;
            DataTable table = ds.Tables[0];
            DataRow[] rowArray = table.Select("");
            if (ef == ExportFormat.ExcelFormat)
            {
                response.ContentType = "application/ms-excel";
                num = 0;
                while (num < (table.Columns.Count - 1))
                {
                    s = s + table.Columns[num].Caption.ToString() + "\t";
                    num++;
                }
                s = s + table.Columns[num].Caption.ToString() + "\n";
                response.Write(s);
                foreach (DataRow row in rowArray)
                {
                    num = 0;
                    while (num < (table.Columns.Count - 1))
                    {
                        str2 = str2 + row[num].ToString().Replace("\t", "").Replace("\n", "").Replace("\r", "") + "\t";
                        num++;
                    }
                    str2 = str2 + row[num].ToString().Replace("\t", "").Replace("\n", "").Replace("\r", "") + "\n";
                    response.Write(str2);
                    str2 = "";
                }
            }
            else if (ef == ExportFormat.XMLFormat)
            {
                response.Write(ds.GetXml());
            }
            response.End();
        }

        public static void ExportDataSetToFile(DataSet ds, string[] cols, ExportFormat ef, string FileName)
        {
            HttpResponse response = HttpContext.Current.Response;
            response.ContentEncoding = Encoding.GetEncoding("GB2312");
            response.AppendHeader("Content-Disposition", "attachment;filename=" + FileName);
            response.Charset = "UTF-8";
            string s = "";
            string str2 = "";
            int index = 0;
            DataTable table = ds.Tables[0];
            DataRow[] rowArray = table.Select("");
            if (ef == ExportFormat.ExcelFormat)
            {
                response.ContentType = "application/ms-excel";
                index = 0;
                while (index < (cols.Length - 1))
                {
                    s = s + table.Columns[cols[index]].Caption.ToString() + "\t";
                    index++;
                }
                s = s + table.Columns[cols[index]].Caption.ToString() + "\n";
                response.Write(s);
                foreach (DataRow row in rowArray)
                {
                    index = 0;
                    while (index < (cols.Length - 1))
                    {
                        if (table.Columns.Contains(cols[index]))
                        {
                            str2 = str2 + row[cols[index]].ToString().Replace("\t", "").Replace("\n", "").Replace("\r", "") + "\t";
                        }
                        index++;
                    }
                    str2 = str2 + row[cols[index]].ToString().Replace("\t", "").Replace("\n", "").Replace("\r", "") + "\n";
                    response.Write(str2);
                    str2 = "";
                }
            }
            else if (ef == ExportFormat.XMLFormat)
            {
                response.Write(ds.GetXml());
            }
            response.End();
        }

        public static void ExportDataSetToFile(string _caption, DataSet ds, ExportFormat ef, string FileName)
        {
            string[] strArray = _caption.Trim().Trim(new char[] { '|' }).Split(new char[] { '|' });
            DataTable table = ds.Tables[0];
            for (int i = 0; i < strArray.Length; i++)
            {
                if (i >= table.Columns.Count)
                {
                    break;
                }
                table.Columns[i].Caption = strArray[i];
            }
            ExportDataSetToFile(ds, ef, FileName);
        }

        public static void ExportDataSetToFile(string[] _caption, DataSet ds, ExportFormat ef, string FileName)
        {
            DataTable table = ds.Tables[0];
            for (int i = 0; i < _caption.Length; i++)
            {
                if (i >= table.Columns.Count)
                {
                    break;
                }
                table.Columns[i].Caption = _caption[i];
            }
            ExportDataSetToFile(ds, ef, FileName);
        }

        public static void ExportDataSetToFile(string _caption, DataSet ds, string[] cols, ExportFormat ef, string FileName)
        {
            string[] strArray = _caption.Trim().Trim(new char[] { '|' }).Split(new char[] { '|' });
            DataTable table = ds.Tables[0];
            for (int i = 0; i < strArray.Length; i++)
            {
                if (i >= table.Columns.Count)
                {
                    break;
                }
                table.Columns[i].Caption = strArray[i];
            }
            ExportDataSetToFile(ds, cols, ef, FileName);
        }

        public static void ExportDataSetToFile(string[] _caption, DataSet ds, string[] cols, ExportFormat ef, string FileName)
        {
            HttpResponse response = HttpContext.Current.Response;
            response.ContentEncoding = Encoding.GetEncoding("GB2312");
            response.AppendHeader("Content-Disposition", "attachment;filename=" + FileName);
            response.Charset = "UTF-8";
            string s = "";
            string str2 = "";
            int index = 0;
            DataTable table = ds.Tables[0];
            DataRow[] rowArray = table.Select("");
            if (ef == ExportFormat.ExcelFormat)
            {
                response.ContentType = "application/ms-excel";
                index = 0;
                while (index < (_caption.Length - 1))
                {
                    s = s + _caption[index] + "\t";
                    index++;
                }
                s = s + _caption[index] + "\n";
                response.Write(s);
                foreach (DataRow row in rowArray)
                {
                    index = 0;
                    while (index < (cols.Length - 1))
                    {
                        if (table.Columns.Contains(cols[index]))
                        {
                            str2 = str2 + row[cols[index]].ToString().Replace("\t", "").Replace("\n", "").Replace("\r", "") + "\t";
                        }
                        index++;
                    }
                    str2 = str2 + row[cols[index]].ToString().Replace("\t", "").Replace("\n", "").Replace("\r", "") + "\n";
                    response.Write(str2);
                    str2 = "";
                }
            }
            else if (ef == ExportFormat.XMLFormat)
            {
                response.Write(ds.GetXml());
            }
            response.End();
        }
    }

}
